$(document).ready(function() {
   // form action
   $.metadata.setType("class");
   jQuery("#review_paper_form").validate({
      submitHandler: function(form) {
         reviewSubmit();
      },
      errorElement: "div",
      errorPlacement: function (error, element) {
         error.appendTo(element.parent().parent());
      }
   });

   function reviewSubmit() {
      $("#review_paper_form").ajaxSubmit({
         dataType: "json",
         beforeSubmit: function(data, form, options) {
            if (!showLoading("#review_paper_form"))
               return false;
            $("#errormsg").hide();
         },
         success: function(data) {
            try {
               var retmsg = data;
               if (retmsg.error) {
                  $("#errormsg").jobUtil("showError", retmsg.error);
               }
               if (retmsg.success) {
                  window.location = retmsg.redirect;
               }
            } catch(e) {
               return false;
            }
         }
      });
   }

   $('#show_all_single').on('click', function(event) {
      $(this).find("i").toggleClass("up");
      $('#show_wrong_single').data('expand', false);
      var target = $(this);
      $.metadata.setType("attr", "data");
      var data = target.metadata();
      var jid = data.jid;
      var sid = data.sid;
      var expand = target.data('expand');

      $.metadata.setType("class");

      var data = {};
      data.jid = jid;
      data.sid = sid;
      data.vtype = 'all';

      if (expand == false) {
         $.ajax({
            type:"GET",
            url: show_paper_url,
            data:data,
            dataType: "html",
            beforeSend: function(xhr){
               showLoading("#select_questionsbox");
            }, success:function(data) {
               if (data) {
                  $('#select_questionsbox').html(data);
                  if (!isLoggedIn(data, target)) {
                     window.location = redirect_url;
                  } else {
                     $('#select_questionsbox').show();
                  }
               } else {
                  $('#select_questionsbox').html('<div class="test_questionbox">用户还未答题</div>').show();
               }
            }
         });
         target.data('expand', true);
         return;
      } else if (expand == true){
         $('#select_questionsbox').hide();
         target.data('expand', false);
         return;
      } else {
         $.ajax({
            type:"GET",
            url: show_paper_url,
            data:data,
            dataType: "html",
            beforeSend: function(xhr){
               showLoading("#select_questionsbox");
            }, success:function(data) {
               if (data) {
                  $('#select_questionsbox').html(data);
                  if (!isLoggedIn(data, target)) {
                     window.location = redirect_url;
                  } else {
                     $('#select_questionsbox').show();
                  }
               } else {
                  $('#select_questionsbox').html('<div class="test_questionbox">用户还未答题</div>').show();
               }
            }
         });
         target.data('expand', true);
         return;
      }
   });

   $('#show_wrong_single').on('click', function(event) {
      $(this).find("i").toggleClass("up");
      var target = $(this);
      $.metadata.setType("attr", "data");
      var data = target.metadata();
      var jid = data.jid;
      var sid = data.sid;
      var expand = target.data('expand');

      $.metadata.setType("class");

      var data = {};
      data.jid = jid;
      data.sid = sid;
      data.vtype = 'wrong';
      $('#show_all_single').data('expand', false);

      if (expand == false) {
         $.ajax({
            type:"GET",
            url: show_paper_url,
            data:data,
            dataType: "html",
            beforeSend: function(xhr){
               showLoading("#select_questionsbox");
            }, success:function(data) {
               if (data) {
                  $('#select_questionsbox').html(data);
                  if (!isLoggedIn(data, target)) {
                     window.location = redirect_url;
                  } else {
                     $('#select_questionsbox').show();
                  }
               } else {
                  $('#select_questionsbox').html('<div class="test_questionbox">没有错题</div>').show();
               }
            }
         });
         target.data('expand', true);
         return;
      } else if (expand == true){
         $('#select_questionsbox').hide();
         target.data('expand', false);
         return;
      } else {
         $.ajax({
            type:"GET",
            url: show_paper_url,
            data:data,
            dataType: "html",
            beforeSend: function(xhr){
               showLoading("#select_questionsbox");
            }, success:function(data) {
               if (data) {
                  $('#select_questionsbox').html(data);
                  if (!isLoggedIn(data, target)) {
                     window.location = redirect_url;
                  } else {
                     $('#select_questionsbox').show();
                  }
               } else {
                  $('#select_questionsbox').html('<div class="test_questionbox">没有错题</div>').show();
               }
            }
         });
         target.data('expand', true);
         return;
      }
   });

   $('#show_freeinput').on('click', function(event) {
      $('#subjective_questionbox').toggle();
   });

});